<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\annotations\builder;

#use \Reflector;
#use tehframework\annotations\DocBlock;
#use tehframework\annotations\DocBlockDefinition;
#use tehframework\annotations\AnnotationDefinition;
#use tehframework\annotations\TagDefinition;
#use tehframework\annotations\AnnotationTargetAware;

class DocBlockBuilderImpl
	extends DocBlockBuilder
	implements AnnotationTargetAware
{
	/**
	 * @var Reflector
	 */
	protected $annotationTarget;
	
	/**
	 * @var AnnotationBuilder
	 */
	protected $annotationBuilder;
	
	/**
	 * @var TagBuilder
	 */
	protected $tagBuilder;

	/**
	 * @param  Reflector $annotationTarget
	 * @return DocBlockBuilderImpl
	 */
	public function setAnnotationTarget(Reflector $annotationTarget)
	{
		$this->annotationTarget = $annotationTarget;
		
		return $this;
	}
	
	/**
	 * @param  AnnotationBuilder $annotationBuilder
	 * @return DocBlockBuilderImpl
	 */
	public function setAnnotationBuilder(AnnotationBuilder $annotationBuilder)
	{
		$this->annotationBuilder = $annotationBuilder;
		
		return $this;
	}
	
	/**
	 * @param  TagBuilder $tagBuilder
	 * @return DocBlockBuilderImpl
	 */
	public function setTagBuilder(TagBuilder $tagBuilder)
	{
		$this->tagBuilder = $tagBuilder;
		
		return $this;
	}
	
	/**
	 * Returns an instance of DocBlock built from the specified definition.
	 * 
	 * @param  DocBlockDefinition $docBlockDefinition
	 *         A definition of annotation to build.
	 * @return DocBlock
	 *         An instance of DocBlock from the specified definition.
	 * @throws BuilderException
	 */
	public function buildDocBlock(DocBlockDefinition $docBlockDefinition)
	{
		$docBlock = new DocBlock(
			$this->buildShortDescription($docBlockDefinition->shortDescription),
			$this->buildLongDescription($docBlockDefinition->longDescription),
			$this->buildTags($docBlockDefinition->tagDefinitions),
			$this->buildAnnotations($docBlockDefinition->annotationDefinitions)
		);
		
		$this->annotationTarget = null;
		
		return $docBlock;
	}
	
	/**
	 * @param  string $description
	 * @return string?
	 */
	protected function buildShortDescription($description)
	{
		return empty($description) ? null : $description;
	}
	
	/**
	 * @param  string $description
	 * @return string?
	 */
	protected function buildLongDescription($description)
	{
		return empty($description) ? null : $description;
	}
	
	/**
	 * @param  TagDefinition[] $defs
	 * @return Tag[]
	 */
	protected function buildTags(array $defs)
	{
		if (($this->tagBuilder === null) || empty($defs))
		{
			return array();
		}
		
		$tags = array();
		
		/* @var $def TagDefinition */
		foreach ($defs as $def)
		{
			if (!isset($tags[$def->name]))
			{
				$tags[$def->name] = array();
			}
			
			$tags[$def->name][] = $this->tagBuilder->buildTag($def);
		}
		
		return $tags;
	}
	
	/**
	 * @param  AnnotationDefinition[] $defs
	 * @param  Reflector $target
	 * @return Annotation[]
	 */
	protected function buildAnnotations(array $defs)
	{
		if (($this->annotationBuilder === null) || empty($defs))
		{
			return array();
		}
		
		$annotations = array();
		
		/* @var $def AnnotationDefinition */
		foreach ($defs as $def)
		{
			if (!isset($annotations[$def->name]))
			{
				$annotations[$def->name] = array();
			}
			
			$annotation = $this->annotationBuilder->buildAnnotation($def);
			
			if ($this->annotationTarget &&
				($annotation instanceof AnnotationTargetAware))
			{
				$annotation->setAnnotationTarget($this->annotationTarget);
			}
			
			$annotations[$def->name][] = $annotation;
		}
		
		return $annotations;
	}
}